package com.lining.orangeservice.controller;

import cn.hutool.core.lang.tree.Tree;
import com.lining.orangeservice.common.Result;
import com.lining.orangeservice.entity.dto.request.PermissionCreateRequest;
import com.lining.orangeservice.entity.dto.request.PermissionUpdateRequest;
import com.lining.orangeservice.entity.po.Permission;
import com.lining.orangeservice.service.PermissionService;
import com.lining.orangeservice.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限管理", description = "权限管理接口")
@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    private final JwtUtil jwtUtil;
    @Operation(summary = "创建权限", description = "创建新的权限，包括菜单、按钮或接口权限")
    @PostMapping("/create")
    public Result<Void> createPermission(@Valid @RequestBody PermissionCreateRequest request) {
        permissionService.createPermission(request);
        return Result.success();
    }

    @Operation(summary = "更新权限", description = "更新现有权限的信息")
    @PutMapping("/update")
    public Result<Void> updatePermission(@Valid @RequestBody PermissionUpdateRequest request) {
        permissionService.updatePermission(request);
        return Result.success();
    }

    @Operation(summary = "删除权限", description = "根据ID删除权限")
    @DeleteMapping("/delete")
    public Result<Void> deletePermission(
            @Parameter(description = "权限ID") @RequestParam Long id) {
        permissionService.deletePermission(id);
        return Result.success();
    }

    @Operation(summary = "获取完整权限树", description = "获取完整的权限树形结构，包括菜单、按钮和接口权限")
    @GetMapping("/tree")
    public Result<List<Tree<Long>>> getPermissionTree() {
        List<Tree<Long>> permissionTree = permissionService.getPermissionTree();
        return Result.success(permissionTree);
    }

    @Operation(summary = "获取当前用户完整权限树", description = "根据当前登录用户获取完整权限树，包括菜单、按钮和接口权限")
    @GetMapping("/my-tree")
    public Result<List<Tree<Long>>> getCurrentUserPermissionTree(HttpServletRequest request) {
        // 从请求头中获取Token并解析用户ID
        Long userId = null;
        String token = jwtUtil.getTokenFromRequest(request);

        if (StringUtils.hasText(token) && jwtUtil.validateToken(token)) {
            userId = jwtUtil.getUserIdFromToken(token);
        }

        if (userId == null) {
            return Result.error(401, "用户未登录");
        }

        List<Tree<Long>> permissionTree = permissionService.getPermissionTreeByUserId(userId);
        return Result.success(permissionTree);
    }

    @Operation(summary = "获取所有权限列表", description = "获取所有权限的扁平列表（包括菜单、按钮、接口等所有类型）")
    @GetMapping("/list")
    public Result<List<Permission>> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        return Result.success(permissions);
    }
}